pkgname=hpcc
pkgver=1.5.0
pkgrel=0
arch=('i386' 'x86_64')
makedepends_debian_11=(build-essential python3 gfortran gcc g++)
source=("http://www.mpich.org/static/downloads/4.1.1/mpich-4.1.1.tar.gz" "https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/hpcc/1.5.0-1/hpcc_1.5.0.orig.tar.gz" "https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.5.tar.gz")
md5sums=('SKIP' 'SKIP' 'SKIP')

build_mpi()
{
        cd $srcdir/mpich-4.1.1
        if [ "$os" == "debian" ] ;then
                ./configure --prefix=/usr/local/mpich FFLAGS=-fallow-argument-mismatch FCFLAGS=-fallow-argument-mismatch
        else
                ./configure --prefix=/usr/local/mpich
        fi
        make && make install
        export PATH=/usr/local/mpich/bin:$PATH
}
build_openmpi()
{
        if [ "$os" == "openeuler" ];then
                cd $srcdir/openmpi-4.1.5
                ./configure --prefix=/usr/local/openmpi
                make && make install
        fi
}
build_blas()
{
        cd $srcdir
        wget http://www.netlib.org/blas/blas.tgz --no-check-certificate
        tar -xzvf blas.tgz
        cd BLAS-3.11.0
        gfortran -c -O3 *.f
        ar rv libblas.a *.o
        make
        cp *.a /usr/local/lib
}
build_cblas()
{
        cd $srcdir
        wget http://www.netlib.org/blas/blast-forum/cblas.tgz --no-check-certificate
        tar -xvzf cblas.tgz
        cd CBLAS
        cp Makefile.LINUX Makefile.in
        if [ "$os" == "debian" ] ;then
                sed -i 's/FFLAGS = -O3/FFLAGS = -O3 -fallow-argument-mismatch/' Makefile.in
        fi
        sed -i 's/BLLIB = libblas.a/BLLIB = \/usr\/local\/lib\/blas_LINUX.a/' Makefile.in
        make
        cp lib/cblas_LINUX.a /usr/local/lib/cblas_LINUX.a
}
build_lapack()
{
        cd $srcdir
        wget http://www.netlib.org/lapack/lapack-3.8.0.tar.gz --no-check-certificate
        tar xzvf lapack-3.8.0.tar.gz
        cd lapack-3.8.0/
        cp make.inc.example make.inc
        if [ "$os" == "debian" ] ;then
                sed -i 's/OPTS    = -O2 -frecursive/OPTS = -O2 -frecursive -fallow-argument-mismatch/g' make.inc
        fi
        sed -i 's|BLASLIB      = ../../librefblas.a|BLASLIB      = /usr/local/lib/blas_LINUX.a|' make.inc
        sed -i 's|CBLASLIB     = ../../libcblas.a|CBLASLIB     = /usr/local/lib/cblas_LINUX.a|' make.inc
        ulimit -s 100000
        if [ "$os" == "debian" ] ;then
                ln -s /usr/bin/python3 /usr/bin/python
        fi
        make
        cp *.a /usr/local/lib
}
build_hpcc()
{
        cd $srcdir/hpcc-1.5.0/
        cp hpl/setup/Make.Linux_PII_CBLAS hpl/Make.name
        cd hpl
        sed -i 's|LAdir        = $(HOME)/netlib/ARCHIVES/Linux_PII|LAdir        = /usr/local/lib|' Make.name
        sed -i 's|LAlib        = $(LAdir)/libcblas.a $(LAdir)/libatlas.a|LAlib        = $(LAdir)/cblas_LINUX.a $(LAdir)/blas_LINUX.a|' Make.name
        sed -i 's|MPdir        = /usr/local/mpi|MPdir        = /usr/local/mpich|' Make.name
        sed -i 's|MPlib        = $(MPdir)/lib/libmpich.a|MPlib        = $(MPdir)/lib/libmpi.a|' Make.name
        if [ "$os" == "debian" ] ;then
                sed -i 's|HPL_LIBS     = $(HPLlib) $(LAlib) $(MPlib) -lm|HPL_LIBS     = $(HPLlib) $(LAlib) $(MPlib) -lm -lpthread -ldl -lrt -latomic|' Make.name
        else
                sed -i 's/static double \* restrict a, \* restrict b, \* restrict c;/static double \* __restrict__ a, \* __restrict__ b, \* __restrict__ c;/g' ../STREAM/stream.c
                sed -i 's|HPL_LIBS     = $(HPLlib) $(LAlib) $(MPlib) -lm|HPL_LIBS     = $(HPLlib) $(LAlib) $(MPlib) -lm -lpthread -ldl -lrt|' Make.name
        fi
        ln -s /usr/local/mpich/include/mpi.h /usr/include/mpi.h
        ln -s /usr/local/mpich/include/mpi_proto.h /usr/include/mpi_proto.h
        ln -s /usr/local/mpich/include/mpio.h /usr/include/mpio.h
        sed -i 's|LINKER       = /usr/bin/g77|LINKER       = /usr/bin/gfortran|' Make.name
        cd ..
        make arch=name
}
build()
{
        build_mpi || return
        build_openmpi || return
        build_blas || return
        build_cblas || return
        build_lapack || return
        build_hpcc
}
package()
{
    mkdir -p "$pkgdir/lkp/benchmarks/hpcc"
    cp -af "$srcdir/hpcc-1.5.0/"* "$pkgdir/lkp/benchmarks/hpcc/"
    if [ "$os" == "openeuler" ];then
        cp -r /usr/local/openmpi $pkgdir/lkp/benchmarks/hpcc/
    fi
}
